package simple; /**
 * @projectName study-note
 * @package PACKAGE_NAME
 * @className PACKAGE_NAME.InorderTraversal
 */

import node.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * InorderTraversal
 * @description 中序遍历
 * @author answer_wx
 * @date 2022/7/7 10:44
 * @version 1.0
 */
public class InorderTraversal {
    public static List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        inorder(root, result);
        return result;
    }

    public static void inorder(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        inorder(root.left, res);
        res.add(root.val);
        inorder(root.right, res);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode();
        root.val = 1;
        root.left = null;
        root.right = new TreeNode(2, new TreeNode(3), null);
        List<Integer> result = inorderTraversal(root);
        for (int i = 0; i < result.size(); i++) {
            System.out.print(result.get(i) + "，");
        }
    }
}


